package com.my.bld.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.my.bld.utils.Pager;
import com.opensymphony.xwork2.ModelDriven;
import java.util.*;

import com.my.bld.model.*;
import com.my.bld.dao.*;
import com.my.bld.service.*;

/**
 * @ClassName:  
 * @Description: 
 * @author administrator
 * @date 2015年12月24日 下午1:46:33 - 2017年02月28日 15时18分02秒
 */

@Controller("cgAction")
@Scope("prototype")
public class CgAction extends ActionSupport implements ModelDriven<Cg>{
	
	private static final long serialVersionUID = 1L;

	//==========model==============
	  private Cg cg;
		@Override
		public Cg getModel() {
			if(cg==null) cg = new Cg();
			return cg;	
		}
		//==========model==============
	/**
	 * 依赖注入 start dao/service/===
	 */
	@Autowired
	private CgService cgService;
	@Autowired
	private ShopService shopService;
	@Autowired
	private KcService kcService;
	private Integer old;
	
	//依赖注入 end  dao/service/===
	
	//-------------------------华丽分割线---------------------------------------------
	
	//============自定义参数start=============
	
	//============自定义参数end=============
	
	//-------------------------华丽分割线---------------------------------------------
	
	public Integer getOld() {
		return old;
	}
	public void setOld(Integer old) {
		this.old = old;
	}
	//============文件上传start=======================================================
	private File file;
	//提交过来的file的名字
    private String fileFileName;
    //提交过来的file的MIME类型
    private String fileContentType;
    public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	 //============文件上传end=========================================================
			
	 //-------------------------华丽分割线---------------------------------------------//
	
	 //=============公=======共=======方=======法==========区=========start============//
	/**
	 * 列表分页查询
	 */
	public String cg(){
	    Map<String,Object> alias = new HashMap<String,Object>();
		StringBuffer sb = new StringBuffer();
		sb = sb.append("from Cg where 1=1 ");
		if(cg!=null && cg.getGs() !=null && !"".equals(cg.getGs() )){
			sb.append("  and gs like :gs ");
			alias.put("gs", "%" +cg.getGs() + "%" );
		}
		sb = sb.append("order by id desc");
		Pager<Cg> pagers = cgService.findByAlias(sb.toString(),alias);
		ActionContext.getContext().put("pagers", pagers);
		ActionContext.getContext().put("Obj", cg);
		return SUCCESS;
    }
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String add(){
		String hql = "from Shop where 1=1 and isDelete = 0";
		List<Shop> shops = shopService.listByAlias(hql, null);
		ActionContext.getContext().put("shops", shops);
		return SUCCESS;
	}
	
	/**
	 * 执行添加
	 * @return
	 */
	public String exAdd(){
		     Calendar now = Calendar.getInstance();  
	/*       System.out.println("年: " + );  
	        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");  
	        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));  
	        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));  
	        System.out.println("分: " + now.get(Calendar.MINUTE));  
	        System.out.println("秒: " + now.get(Calendar.SECOND));  */
	        String year = now.get(Calendar.YEAR)+"";
	        String yue =  (now.get(Calendar.MONTH) + 1) + "";
	        String ri = now.get(Calendar.DAY_OF_MONTH)+""; 
	        //下面搞定库存
		    //首先判断这个shop在库存中有没有
		    String hql = "from Kc where shop.id = "+cg.getShop().getId();
		    Map<String ,Object> map = new HashMap<>();
		    map.put("id", cg.getShop().getId());
		    Kc byId = (Kc) kcService.queryObject(hql);
		    if(byId == null){
		    	Kc Kc = new Kc();
		    	Kc.setShop(shopService.getById(cg.getShop().getId()));
		    	Kc.setTotalNum(cg.getNum());
		    	kcService.save(Kc);
		    }else{
		    	byId.setTotalNum(byId.getTotalNum()+cg.getNum());
		    	kcService.update(byId);
		    }
	        System.out.println(now.getTime()); 
	        cg.setIsDelete(0); 
	        cg.setYear(year);
	        cg.setYue(yue);
	        cg.setRi(ri);
	        cg.setCreateTime(new Date());
		    cgService.save(cg);
		   
		    ActionContext.getContext().put("url", "/cg_cg.do");
		return "redirect";
	}
	
	/**
	 * 查看详情页面
	 * @return
	 */
	public String view(){
		Cg n = cgService.getById(cg.getId());
		ActionContext.getContext().put("Obj", n);
		return SUCCESS;
	}
	
	/**
	 * 退货
	 * @return
	 */
	public String th(){
		Cg n = cgService.getById(cg.getId());
		ActionContext.getContext().put("Obj", n);
		return SUCCESS;
	}
	
	/**
	 * 退货
	 * @return
	 */
	public String fth(){
		Cg n = cgService.getById(cg.getId());
		ActionContext.getContext().put("Obj", n);
		return SUCCESS;
	}
	/**
	 * 跳转修改页面
	 * @return
	 */
	public String update(){
		Cg n = cgService.getById(cg.getId());
		ActionContext.getContext().put("Obj", n);
		return SUCCESS;
	}
    
	/**
	 * 执行修改
	 * @return
	 */
	public String exUpdate(){
		//修改之前，先把仓库原来的删掉old个
		Cg n = cgService.getById(cg.getId());
		 String hql = "from Kc where shop.id = "+n.getShop().getId();
		    Map<String ,Object> map = new HashMap<>();
		    Kc byId = (Kc) kcService.queryObject(hql);
		    if(byId == null){
		    }else{
		    	byId.setTotalNum(byId.getTotalNum() - old);
		    	
		    }
		n.setGs(cg.getGs());
		n.setNum(cg.getNum());
		n.setCbj(cg.getCbj());
		byId.setTotalNum(byId.getTotalNum()+cg.getNum());
		kcService.update(byId);
		cgService.update(n);
		ActionContext.getContext().put("url", "/cg_cg.do");
		return "redirect";
	}
	
	
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		Cg n = cgService.getById(cg.getId());
		 String hql = "from Kc where shop.id = "+n.getShop().getId();
		    Map<String ,Object> map = new HashMap<>();
		    Kc byId = (Kc) kcService.queryObject(hql);
		    if(byId == null){
		    }else{
		    	byId.setTotalNum(byId.getTotalNum() - n.getNum());
		    	kcService.update(byId);
		    }
		
		n.setIsDelete(1);
		n.setThly(cg.getThly());
		cgService.update(n);
		ActionContext.getContext().put("url", "/cg_cg.do");
		return "redirect";
	}
	
	//=============公=======共=======方=======法==========区=========end============//
	
	 //-------------------------华丽分割线---------------------------------------------//
	
	 //=============自=======定=======义=========方=======法==========区=========start============//
	
	
	
	
	//=============自=======定=======义=========方=======法==========区=========end============//
		
	
	
}
